<template>
  <div class="tools-li">
    <transition name="el-zoom-in-right">
      <div
        v-show="isShow"
        :key="tooltipId"
        ref="customAlert"
        class="custom-demo"
        :style="customDemo"
        transiton="el-zoom-in-center"
      >
        <div
          v-for="(item,index) in dataList"
          :key="'the-'+index"
          class="custom-ul-li"
          :class="item.value === $store.getters.theme ? 'active': ''"
          @click="doClick(item)"
          @mouseover="mousemove(item)"
          @mouseleave="mouseleave()"
        >{{ item.name }}</div>
      </div>
    </transition>
    <slot name="theme" />
  </div>
</template>

<script>
import { generateId } from 'element-ui/src/utils/util'
export default {
  name: 'Theme',
  props: {
    trigger: {
      type: String,
      default: 'click',
      validator: value => ['click', 'focus', 'hover', 'manual'].indexOf(value) > -1
    }
  },
  data() {
    return {
      isShow: false,
      customDemo: {},
      customMenu: {},
      dataList: [
        {
          name: '极光蓝',
          value: 'default'
        },
        {
          name: '阳光橙',
          value: 'orange'
        },
        {
          name: '梅子红',
          value: 'red'
        },
        {
          name: '开源绿',
          value: 'green'
        },
        {
          name: '极光紫',
          value: 'purple'
        },
        {
          name: '深黑色',
          value: 'black'
        }
      ],
      mouseId: null
    }
  },
  computed: {
    tooltipId() {
      return `el-custom-user-${generateId()}`
    }
  },
  mounted() {
    if (this.trigger === 'click' && undefined !== this.$refs.customAlert) { // click 事件
      this.$refs.customAlert.addEventListener('click', this.triggerOnClick)
    }
    if (this.trigger === 'hover' && undefined !== this.$refs.customAlert) {
      this.$refs.customAlert.addEventListener('mouseenter', this.triggerOpen)
      this.$refs.customAlert.addEventListener('mouseleave', this.triggerClose)
    }
  },
  destroyed() {
    if (this.trigger === 'click' && undefined !== this.$refs.customAlert) { // click 事件
      this.$refs.customAlert.removeEventListener('click', this.triggerOnClick)
    }
    if (this.trigger === 'hover' && undefined !== this.$refs.customAlert) {
      this.$refs.customAlert.removeEventListener('mouseenter', this.triggerOpen)
      this.$refs.customAlert.removeEventListener('mouseleave', this.triggerClose)
    }
  },
  methods: {
    doClick(data) {
      this.$confirm('确定切换为：【' + (data.name === undefined ? '默认主题' : data.name) + '】主题, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$store.commit('SET_THEME', data.value)
        this.triggerClose()
      }).catch(() => {
        this.$message.success('取消成功！')
      })
    },
    triggerOnClick() {
      if (this.isShow) {
        this.isShow = false
      } else {
        this.isShow = true
      }
    },
    triggerOpen() {
      this.isShow = true
      this.$emit('callBack', this.isShow)
    },
    triggerClose() {
      this.isShow = false
      this.$emit('callBack', this.isShow)
    },
    mousemove(data) {
      this.mouseId = data.id
    },
    mouseleave() {
      this.mouseId = null
    }
  }
}
</script>

<style scoped>
.tools-li {
  width: 100%;
  height: 100%;
  position: relative;
}
.custom-demo {
  position: absolute;
  top: -197px;
  right: 67px;
  height: auto;
  padding: 10px 0;
  box-sizing: border-box;
  width: 120px;
  z-index: 100;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border-radius: 4px;
  background-color: #ffffff;
}
.custom-ul-li {
  width: 100%;
  height: 40px;
  line-height: 40px;
  font-size: 0.8rem;
  cursor: pointer;
  text-align: center;
  background: #fff;
  color:black;
  padding: 0 10px;
  box-sizing: border-box;
}
.custom-ul-li.active {
  background-color: #e4e4e4;
}
.custom-ul-li:hover {
  background-color: #e1e1e1;
}
</style>
